package com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import conexionjdbc.ConexionSqlServer;

public class ActualizarSistemaDAO {

	public void executeJDBCCreateFuntion() {

		String conSql;
		Statement stm = null;
		Connection conecction;

		try {

			conecction = ConexionSqlServer.crearConexion();
			stm = conecction.createStatement();

			conSql = "IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[funGetFillZero]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) " +
					"DROP FUNCTION [dbo].[funGetFillZero] ";

			stm.execute(conSql);

			conSql = "create function [dbo].[funGetFillZero] (@campo varchar(15), @canCar int) \n" +
					"returns varchar(15) \n" +
					"AS \n" +
					"BEGIN \n" +
					"	declare @ite int, @nuevocampo varchar(15), @totalIte int \n" +
					"	set @nuevocampo = @campo \n" +
					"	set @ite = 1 \n" +
					"	if(isnumeric(@campo)=1)begin \n" +
					"		if(@canCar > len(@campo)) begin \n" +
					"			set @totalIte = @canCar - len(@campo) \n" +
					"		end \n" +
					"		else begin \n" +
					"			set @totalIte = 0 \n" +
					"		end \n" +
					"		while(@ite <= @totalIte) begin \n" +
					"			set @nuevocampo = '0' + @nuevocampo \n" +
					"			set @ite = @ite + 1 \n" +
					"		end \n" +
					"	end \n" +
					"	return @nuevocampo \n" +
					"END ";

			stm.execute(conSql);
			conecction.close();

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void ejecutarSentencias(String archivo) {

		Statement stm = null;
		Connection conecction;

		List<String> listSql = leerArchivo(archivo);
		for (String iteSql : listSql) {

			conecction = ConexionSqlServer.crearConexion();

			try {

				stm = conecction.createStatement();
				stm.execute(iteSql);
				conecction.close();

			} catch (SQLException e) {
				throw new RuntimeException(e);
			}

		}
	}

	public String leerFuente() {

		String fuente = "";
		try {

			FileReader file = new FileReader("src/ManejoCaracter/LeerFuente.java");
			BufferedReader buff = new BufferedReader(file);
			boolean eof = false;
			while (!eof) {
				String line = buff.readLine();
				if (line == null)
					eof = true;
				else
					// System.out.println(line);
					fuente = fuente + line + "\n";
			}

			buff.close();

		} catch (IOException e) {
			System.out.println("Error -- " + e.toString());
		}

		return fuente;

	}

	public List<String> leerArchivo(String archivo) {

		String archivoSql = new String();
		List<String> listSql = new ArrayList<String>();

		try {

			File file = new File(archivo);
			BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
			String linea = "";
			try {
				while (bufferedReader.ready()) {

					linea = bufferedReader.readLine();
					if (linea.trim().toUpperCase().equals("GO")) {

						listSql.add(archivoSql);
						archivoSql = "";

					} else {
						archivoSql = archivoSql + linea + "\n";
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Ejecutando sql... " + archivo);
		return listSql;
	}

}
